package com.FH.applyForm.officeServies.noticeform_type;

import com.FH.applyForm.officeServies.carManage_firstType.CarManageShowDTO;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/31.
 */
@Stateless
public class NoticeFormTypeDAOImpl extends AccessDB implements NoticeFormTypeDAO {

    /**
     * 查询发文类型
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getNoticeFormTypeInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from noticeformtype  where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("id")) {
                sql_w += " AND id ='" + json.getString("id") + "'";
            }
            if (json.has("noticeform_type")) {
                sql_w += " AND noticeform_type ='" + json.getString("noticeform_type") + "'";
            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();

            List<NoticeFormTypeShowDTO> result = new ArrayList<NoticeFormTypeShowDTO>();

            if (!existName.equals("0")) {
                String sql = "SELECT id,noticeform_type FROM noticeformtype WHERE 1=1 " + sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    NoticeFormTypeShowDTO noticeFormTypeShowDTO = new NoticeFormTypeShowDTO();
                    String id = seresult.get(i)[1];
                    noticeFormTypeShowDTO.setId(id);
                    noticeFormTypeShowDTO.setNoticeform_type(seresult.get(i)[2]);

                    result.add(noticeFormTypeShowDTO);

                }
            }
            if (seresult.size() != 0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }


    /**
     * 添加发文类型
     * @param noticeFornTypePullDTO
     * @return
     */
    @Override
    public ResultDTO addNoticeFormTypeInfo(NoticeFormTypePullDTO noticeFornTypePullDTO) {
        int resultls = 0;
        ResultDTO resultDTO = new ResultDTO();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String noticeform_type = noticeFornTypePullDTO.getNoticeform_type();
        String sql = "select * from noticeformtype where noticeform_type='" + noticeform_type + "'";
        String sum = getReportingToOne(sql);
        if (!sum.equals("")) {

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("信息已存在，请重新分配");
            return resultDTO;
        }
        String sql1 = "";
        sql1 = "insert into noticeformtype (id,noticeform_type)" +
                "values('" + uuid + "','" + noticeFornTypePullDTO.getNoticeform_type() + "')";

        resultls = addReportingTo(sql1);
        if (resultls != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


    /**
     * 修改发文类型
     * @param noticeFornTypePullDTO
     * @return
     */
    @Override
    public ResultDTO updateNoticeFormTypeInfo(NoticeFormTypePullDTO noticeFornTypePullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        //更新sql语句
        QUERY += "update advance_type set ";
        if (null != noticeFornTypePullDTO.getNoticeform_type() && !"".equals(noticeFornTypePullDTO.getNoticeform_type())) {
            QUERY += " noticeform_type = '" + noticeFornTypePullDTO.getNoticeform_type() + "'";
        }

        QUERY += " where id = '" + noticeFornTypePullDTO.getId() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion("发文类型信息更新成功！");
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("发文类型信息更新失败！");
        }
        return resultDTO;


    }

    /**
     * 删除发文类型
     * @param noticeFornTypePullDTO
     * @return
     */
    @Override
    public ResultDTO deleteNoticeFormTypeInfo(NoticeFormTypePullDTO noticeFornTypePullDTO) {
        String sql="select count(1) from noticeformtype WHERE id='"+noticeFornTypePullDTO.getId()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(existadvance!=null){
            String QUERY = "";
            QUERY += "delete from noticeformtype  where  id='"+noticeFornTypePullDTO.getId()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion("发文类型信息删除成功！");
        }else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("发文类型信息删除失败！");
        }
        return resultDTO;
    }
}
